package com.zheng.pay.admin.manage.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.validator.LengthValidator;
import com.zheng.pay.common.constant.PayResult;
import com.zheng.pay.common.constant.PayResultConstant;
import com.zheng.pay.dao.model.PayMchInfo;
import com.zheng.pay.dao.model.PayMchInfoExample;
import com.zheng.pay.rpc.api.PayMchInfoService;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "商户信息管理", description = "商户信息管理")
@RequestMapping("/manage/mchinfo")
public class MchInfoController  extends BaseController{

    private static final Logger _log = LoggerFactory.getLogger(MchInfoController.class);

    @Autowired
    private PayMchInfoService payMchInfoService;
    @Autowired
    private UpmsApiService upmsApiService;

    @ApiOperation(value = "商户信息首页")
    @RequiresPermissions("pay:mchinfo:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/mchinfo/index.jsp";
    }

    @ApiOperation(value = "商户信息列表")
    @RequiresPermissions("pay:mchinfo:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {

        PayMchInfoExample payMchInfoExample= new PayMchInfoExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            payMchInfoExample.setOrderByClause(sort + " " + order);
        }
        List<PayMchInfo> rows = payMchInfoService.selectByExampleForOffsetPage(payMchInfoExample, offset, limit);
        long total = payMchInfoService.countByExample(payMchInfoExample);
        Map<String, Object> result = new HashMap<>(2);
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增")
    @RequiresPermissions("pay:mchinfo:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        //代理类型
        List<UpmsDict> agentTypeList = upmsApiService.selectUpmsDictByCode(Constant.UPMS_AGENT_TYPE);
        modelMap.put("agentTypeList", agentTypeList);
        return "/manage/mchinfo/create.jsp";
    }

    @ApiOperation(value = "新增商户信息")
    @RequiresPermissions("pay:mchinfo:create")
    @RequestMapping(value = "/create", method = { RequestMethod.POST})
    @ResponseBody
    public Object create(PayMchInfo payMchInfo) {
        Date time = DateUtil.getNowDate();
        payMchInfo.setUpdatetime(time);
        payMchInfo.setCreatetime(time);
        int count = payMchInfoService.insertSelective(payMchInfo);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除商户信息")
    @RequiresPermissions("pay:mchinfo:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = payMchInfoService.deleteByPrimaryKeys(ids);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改商户信息")
    @RequiresPermissions("pay:mchinfo:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") String id, ModelMap modelMap) {

        PayMchInfoExample payMchInfoExample= new PayMchInfoExample();
        if (!StringUtils.isBlank(id) && !StringUtils.isBlank(id)) {
            payMchInfoExample.createCriteria().andMchidEqualTo(id);
        }
        PayMchInfo payMchInfo = payMchInfoService.selectFirstByExample(payMchInfoExample);
        modelMap.put("payMchInfo", payMchInfo);
        //代理类型
        List<UpmsDict> agentTypeList = upmsApiService.selectUpmsDictByCode(Constant.UPMS_AGENT_TYPE);
        modelMap.put("agentTypeList", agentTypeList);
        return "/manage/mchinfo/update.jsp";
    }

    @ApiOperation(value = "修改商户信息")
    @RequiresPermissions("pay:mchinfo:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") String id, PayMchInfo payMchInfo) {
        payMchInfo.setMchid(id);
        Date time = DateUtil.getNowDate();
        payMchInfo.setUpdatetime(time);
        int count = payMchInfoService.updateByPrimaryKeySelective(payMchInfo);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }


}